package com.huobao.yunying.adapters;

import com.huobao.yunying.entity.My_address;
import com.huobao.zhangying.R;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TextView;
import joanzapata.android.BaseAdapterHelper;
import joanzapata.android.QuickAdapter;

public class Adapter_Address extends QuickAdapter<My_address> implements OnCheckedChangeListener{

	private RadioButton rb_newAddress;
	private View newAddressView;
	public Adapter_Address(Context context,RadioButton rb_newAddress,View newAddressView) {
		super(context, R.layout.item_shouhuoren);
		this.rb_newAddress = rb_newAddress;
		this.newAddressView = newAddressView;
		this.rb_newAddress.setOnCheckedChangeListener(this);
	}

	@Override
	protected void convert(BaseAdapterHelper helper, final My_address data) {
		final TextView rb_default_address = helper.getView(R.id.rb_default_address);
		TextView tv_detail_address = helper.getView(R.id.tv_detail_address);
		rb_default_address.setText("收货人：" + data.consignee + "(" + data.phone_mob + ")");
		tv_detail_address.setText(data.region_name+" "+data.address);
		if (data.isSelect) {
			rb_default_address.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.add_title_news_checked, 0, 0, 0);
		} else {
			rb_default_address.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.add_title_news_notcheck, 0, 0, 0);
		}
		rb_default_address.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (data.isSelect)
					return;
				for (My_address address : getList()) {
					address.isSelect = false;
					if(address.addr_id.equals(data.addr_id)){
						address.isSelect = true;
						rb_newAddress.setChecked(false);
					}
				}
				notifyDataSetChanged();
			}
		});
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(isChecked){
			for (My_address address : getList()) {
				address.isSelect = false;
			}
			notifyDataSetChanged();
			newAddressView.setVisibility(View.VISIBLE);
		}else{
			newAddressView.setVisibility(View.GONE);
		}
	}

	public interface OnNewAddressCheckChangeListener{
		void onChangeed(boolean isChecked);
	}
}
